﻿using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games.FortyThievesVariants
{
    [Game("854E624F-D01E-4DD4-A2EF-A02E2546D4A6", LevelOfDifficulty.Easy, LevelOfSkill.MostlySkill, DurationOfPlay.MoreThan10Minutes, NumberOfDecks = 2, Family = FamilyName)]
    public class FortyThieves : StandardCardGame
    {
        public const string FamilyName = "Forty Thieves";

        public FortyThieves()
        {
            NamesOfAdditionalPiles.AddRange(new[] {Stock, Waste});

            CardsPerDeal = 1;
            MaximumDeals = 1;

            TotalDecks = 2;
            TotalFoundations = 8;
            TotalTableaus = 10;
            TableauDealFormat = "4 up";
        }

        public override ILayoutSite CreatePresentation(IPresentationContext presentation)
        {
            var metrics = presentation.Metrics;

            var root = new GridLayout
            {
                Columns = 1,
                Rows = 2,
                HorizontalContentAlignment = HorizontalAlignment.Center
            };

            {
                var topRow = new GridLayout
                             {
                                 Columns = 3,
                                 Rows = 1,
                             };

                {
                    var stockWaste = new SideBySideLayout
                                     {
                                         presentation.CreatePileVisual(Stock, CreateStockStyle(metrics)),
                                         presentation.CreatePileVisual(Waste, presentation.StyleForWaste())
                                     };

                    topRow.Add(stockWaste);
                }

                {
                    topRow.Add(presentation.CreateMimimalSpacer());
                }

                {
                    var foundations = presentation.CreateRowLayout(HorizontalAlignment.Center); // CreateSideBySideLayout(metrics);
                    topRow.Add(foundations);

                    presentation.AddPiles(foundations, NamesOfFoundations, presentation.StyleForRepeatedRankFoundation(this, FoundationBaseRank, TotalDecks));
                }

                root.Add(topRow);
            }

            {
                var tableau = presentation.CreateRowLayout(); // CreateSideBySideLayout(metrics);

                foreach (var pile in PilesNamed(AnyTableau))
                    tableau.Add(presentation.CreatePileVisual(pile.Name, StandardStyles.Tableau));

                //OnTableauLayoutCreated(presentation, tableau);
                root.Add(tableau);
            }

            return root;
        }

        protected bool DealFoundationsInSetup { get; set; }

        protected string TableauDealFormat { get; set; }

        protected override IDealAction CreateDealScript()
        {
            var script = new DealScript();

            PrepareDealScript(script);

            foreach (var name in NamesOfTableaus)
                script.Deal(TableauDealFormat).To(name);

            script.DealRemainingFaceDownTo(Stock);

            return script.Compile();
        }

        protected override void PrepareDealScript(DealScript script)
        {
            if (DealFoundationsInSetup) script.DealRankTo(NamesOfFoundations, FoundationBaseRank);
        }

        protected override bool AllowedToPickupAtTableau(RemovePileAction action)
        {
            return action.IsActingOnFaceUpTopCard();
        }

        protected override bool AllowedToBuildNonEmptyTableau(MovePileAction action)
        {
            return action.PileBeingMoved.BottomCard.Rank != FoundationBaseRank && base.AllowedToBuildNonEmptyTableau(action);
        }
    }
}
